package com.swhy

import java.net.URI
import java.util.{Date, UUID}

import com.utils._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import org.codehaus.jackson.map.ObjectMapper


object ChgeAcctIndxHistory2 {
  def main(args: Array[String]): Unit = {
    if (args.length != 3) {
      println(
        """
          |com.swhy.CompanyInfo
          |params:
          | InputDate    刷数日期
          | OutputPath   输出路径
          | SplitNum     文件生成数量
        """.stripMargin)
      sys.exit()
    }
    val conf = new Configuration()
    val Array(inputDt, outputPath, splitNum) = args
    val sparkConf = new SparkConf().setAppName(this.getClass.getName)
    val sc = new SparkContext(sparkConf)
    val hiveContext = new HiveContext(sc)
//改下方的sql为需要装换成jason的sql
    val allcompany =s"""
        |select
        |ecif_cd,
        |cust_no,
        |id as capi_acc,
        |tm,
        |indx_name,
        |indx_no,
        |indx_type,
        |indx_val,
        |tm_flag_type,
        |tm_flag
        |from
        |app_data.app_dao_acct_ind_q
        |where id_type='a0101'
        |and
        |((tm_flag_type = 'D' and tm_flag = '${inputDt}')
        |or( tm_flag_type = 'M' and tm_flag = substr('${inputDt}',1,6))
        |or( tm_flag_type = 'Y' and tm_flag = substr('${inputDt}',1,4)))
      """.stripMargin
    print(allcompany)
    val frame = hiveContext.sql(allcompany)
    val result = frame.rdd.map(x => {
      var cominfo = new StringBuilder()

      val ecif_cd = x.getAs[String]("ecif_cd")
      val cust_no = x.getAs[String]("cust_no")
      val capi_acc = x.getAs[String]("capi_acc")
      val tm = x.getAs[Date]("tm")
      val indx_name = x.getAs[String]("indx_name")
      val indx_no = x.getAs[String]("indx_no")
      val indx_type = x.getAs[Integer]("indx_type")
      val indx_val = x.getAs[String]("indx_val")
      val tm_flag_type = x.getAs[String]("tm_flag_type")
      val tm_flag = x.getAs[String]("tm_flag")

      val _id = new ID()
      val index = new Index()
      val companyInfo = new ChgeAcctIndxHistory()

      _id.set_id(UUID.randomUUID().toString)
      index.setIndex(_id)
      companyInfo.setEcifCd(ecif_cd)
      companyInfo.setCustNo(cust_no)
      companyInfo.setCapiAcc(capi_acc)
      companyInfo.setTm(tm)
      companyInfo.setIndxName(indx_name)
      companyInfo.setIndxNo(indx_no)
      companyInfo.setIndxType(indx_type)
      companyInfo.setIndxVal(indx_val)
      companyInfo.setTmFlagType(tm_flag_type)
      companyInfo.setTmFlag(tm_flag)

      val mapper = new ObjectMapper()

      val indexJson = mapper.writeValueAsString(index) //企业自增编码json串
      val companyInfoJson = mapper.writeValueAsString(companyInfo) //企业信息串

      cominfo = cominfo.append(indexJson + "\n" + companyInfoJson) //json结果串
      cominfo.toString()
    })
    val path = new Path(outputPath) //创建文本输出路径
    val fs = FileSystem.get(URI.create(outputPath), conf)
    if (fs.exists(path)) {
      fs.delete(path, true)
    }
    result.coalesce(splitNum.toInt,true).saveAsTextFile(outputPath)
    sc.stop()
  }
}
